//
//  MonthReportController.m
//  YongChuanTradingArea
//
//  Created by 李冰 on 2017/9/13.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "MonthReportController.h"
#import "MonthReportTableCell.h"
#import "MonthTimePicker.h"
#import "MonthReportModel.h"

static NSString * const MonthReportTableCellIde = @"MonthReportTableCellIde";
@interface MonthReportController ()<MonthTimePickerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *startTime;
@property (weak, nonatomic) IBOutlet UIButton *endTime;

@property (nonatomic, assign) NSInteger startYear;
@property (nonatomic, assign) NSInteger startMonth;
@property (nonatomic, assign) NSInteger startDay;
@property (nonatomic, assign) NSInteger endYear;
@property (nonatomic, assign) NSInteger endMonth;
@property (nonatomic, assign) NSInteger endDay;

@property (nonatomic, assign) BOOL setStart;

@end

@implementation MonthReportController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _startTime.layer.cornerRadius = 15;
    _endTime.layer.cornerRadius = 15;
    
    self.startYear = 2017;
    self.startMonth = 1;
    self.endYear = 2017;
    if (self.isMonth) {
        self.endMonth = 8;
        self.title = @"月报表";
    }else{
        self.startDay = 1;
        self.endMonth = 1;
        self.endDay = 1;
        self.title = @"日报表";
    }
    self.tableView.rowHeight = 60;
    [self.tableView registerNib:[UINib nibWithNibName:@"MonthReportTableCell" bundle:nil] forCellReuseIdentifier:MonthReportTableCellIde];
    self.tableView.frame = CGRectMake(0, 120, lbScreenWidth, lbScreenHeight - 120 - 64);
    [self.view addSubview:self.tableView];
    
    [self setupPageHeader:YES footer:YES];
    [self headerRefresh];
}

- (void)requestPageData:(void (^)(YCNetworkingStatusCode, NSArray *))statusBlock {
    NSString *startTime = @"";
    NSString *endTime = @"";
    if (self.isMonth) {
        startTime = [NSString stringWithFormat:@"%ld/%02ld", _startYear, _startMonth];
        endTime = [NSString stringWithFormat:@"%ld/%02ld", _endYear, _endMonth];
    }else{
        startTime = [NSString stringWithFormat:@"%ld/%02ld/%02ld", _startYear, _startMonth, _startDay];
        endTime = [NSString stringWithFormat:@"%ld/%02ld/%02ld", _endYear, _endMonth, _endDay];
    }
    //    memberId={memberId}&startTime={startTime}&endTime={endTime}
    [LBNetworkingInstance requestGET:self.isMonth? memberMonthStatistics:memberDayStatistics parameters:@{@"memberId":[LBUserManage defaultManager].memberId,@"startTime":startTime,@"endTime":endTime} showHUD:YES success:^(id responseObject, NSInteger code, NSString *message) {
        
        if (code == YCNetworkingStatusCodeSuccess)
        {
            NSArray *array = [NSArray yy_modelArrayWithClass:MonthReportModel.class json:responseObject[@"Data"]];
            statusBlock(code, array);
            [self.tableView reloadData];
        }
        else
        {
            statusBlock(code, nil);
        }
        
        
    }failure:^(NSError *err) {
        
        statusBlock(YCNetworkingStatusCodeHTTPError, nil);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    MonthReportModel *model = self.dataSource[indexPath.item];
    
    MonthReportTableCell *cell = [tableView dequeueReusableCellWithIdentifier:MonthReportTableCellIde forIndexPath:indexPath];
    cell.timeLabel.text = model.StaDate;
    cell.saleOrder.text = model.SellTotalCount;
    cell.verifyOrder.text = model.VerifyTotalCount;
    cell.salePrice.text = model.SellTotalMoney;
    cell.rewardPrice.text = model.RewardTotalMoney;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (IBAction)startTimeAction:(UIButton *)sender {
    
    self.setStart = YES;
    MonthTimePicker *picker = [[MonthTimePicker alloc] init];
    picker.delegate = self;
    if (!self.isMonth) {
        picker.hasDay = YES;
    }
    [picker show];
}
- (IBAction)endTimeAction:(UIButton *)sender {
    
    self.setStart = NO;
    MonthTimePicker *picker = [[MonthTimePicker alloc] init];
    picker.delegate = self;
    if (!self.isMonth) {
        picker.hasDay = YES;
    }
    [picker show];
}

- (void)pickerDate:(MonthTimePicker *)pickerDate year:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {

    if (self.setStart)
    {
        self.startYear = year;
        self.startMonth = month;
        self.startDay = day;
        [self headerRefresh];
    }
    else
    {
        self.endYear = year;
        self.endMonth = month;
        self.endDay = day;
        [self headerRefresh];
    }
}

- (void)setStartYear:(NSInteger)startYear {
    _startYear = startYear;
    if (self.isMonth) {
        [self.startTime setTitle:[NSString stringWithFormat:@"%ld/%02ld", _startYear, _startMonth] forState:UIControlStateNormal];
    }else{
        [self.startTime setTitle:[NSString stringWithFormat:@"%ld/%02ld/%02ld", _startYear, _startMonth, _startDay] forState:UIControlStateNormal];
    }
}

- (void)setStartMonth:(NSInteger)startMonth {
    
    _startMonth = startMonth;
    if (self.isMonth) {
        [self.startTime setTitle:[NSString stringWithFormat:@"%ld/%02ld", _startYear, _startMonth] forState:UIControlStateNormal];
    }else{
        [self.startTime setTitle:[NSString stringWithFormat:@"%ld/%02ld/%02ld", _startYear, _startMonth, _startDay] forState:UIControlStateNormal];
    }
}

- (void)setStartDay:(NSInteger)startDay{
    _startDay = startDay;
    if (self.isMonth) {
        [self.startTime setTitle:[NSString stringWithFormat:@"%ld/%02ld", _startYear, _startMonth] forState:UIControlStateNormal];
    }else{
        [self.startTime setTitle:[NSString stringWithFormat:@"%ld/%02ld/%02ld", _startYear, _startMonth, _startDay] forState:UIControlStateNormal];
    }
}

- (void)setEndYear:(NSInteger)endYear {
    
    _endYear = endYear;
    if (self.isMonth) {
        [self.endTime setTitle:[NSString stringWithFormat:@"%ld/%02ld", _endYear, _endMonth] forState:UIControlStateNormal];
    }else{
        [self.endTime setTitle:[NSString stringWithFormat:@"%ld/%02ld/%02ld", _endYear, _endMonth, _endDay] forState:UIControlStateNormal];
    }
    
}

- (void)setEndMonth:(NSInteger)endMonth {
    
    _endMonth = endMonth;
    if (self.isMonth) {
        [self.endTime setTitle:[NSString stringWithFormat:@"%ld/%02ld", _endYear, _endMonth] forState:UIControlStateNormal];
    }else{
        [self.endTime setTitle:[NSString stringWithFormat:@"%ld/%02ld/%02ld", _endYear, _endMonth, _endDay] forState:UIControlStateNormal];
    }
}

- (void)setEndDay:(NSInteger)endDay{
    _endDay = endDay;
    if (self.isMonth) {
        [self.endTime setTitle:[NSString stringWithFormat:@"%ld/%02ld", _endYear, _endMonth] forState:UIControlStateNormal];
    }else{
        [self.endTime setTitle:[NSString stringWithFormat:@"%ld/%02ld/%02ld", _endYear, _endMonth, _endDay] forState:UIControlStateNormal];
    }
}
@end
